package com.st.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.cj.util.StringUtils;
import com.st.mall.product.dao.CategoryBrandRelationDao;
import com.st.mall.product.entity.AttrGroupEntity;
import com.st.mall.product.entity.CategoryBrandRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.common.utils.PageUtils;
import com.st.common.utils.Query;

import com.st.mall.product.dao.BrandDao;
import com.st.mall.product.entity.BrandEntity;
import com.st.mall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
   private CategoryBrandRelationDao dao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<BrandEntity>();
        //如果key不为空，添加模糊查询条件
        if (!StringUtils.isNullOrEmpty(key)) {
            wrapper.lambda().eq(BrandEntity::getBrandId, key)
                    .or().like(BrandEntity::getName, key)
                    .or().like(BrandEntity::getFirstLetter, key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateMore(BrandEntity brand) {
        baseMapper.updateById(brand);
        //同步更新CategoryBrandRelationEntity对应表的brandName
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setBrandId(brand.getBrandId());
        dao.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelationEntity>().lambda()
                .eq(CategoryBrandRelationEntity::getBrandId, brand.getBrandId()));
        //TODO 其他关联更新
    }

}